// cSharePointSettingsPage.cpp : implementation file
//

#include "stdafx.h"
#include "qpmig.h"
#include "cSharePointSettingsPage.h"
#include "helper.h"
extern CQPMigApp theApp;

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// cSharePointSettingsPage property page

IMPLEMENT_DYNCREATE(cSharePointSettingsPage, CPropertyPageEx)

cSharePointSettingsPage::cSharePointSettingsPage() : CPropertyPageEx(cSharePointSettingsPage::IDD,0,IDS_SPSSETTINGSHEADERTITLE,IDS_SPSSETTINGSHEADERSUBTITLE)
{
	//{{AFX_DATA_INIT(cSharePointSettingsPage)
	m_strContainerType = _T("Site");
	m_strOwnerAccount = theApp.stControl.m_strOwnerAccount;
	m_strOwnerEMail = theApp.stControl.m_strOwnerEMail;
	m_strLDAPDirectory = theApp.stControl.m_strLDAPPath;
	m_strSiteContainerRoot = _T("Sites");
	m_bShowInSiteDirectory = TRUE;
	m_strUserDataFileName = theApp.stControl.m_strUserDataFileName;
	//}}AFX_DATA_INIT
}

cSharePointSettingsPage::~cSharePointSettingsPage()
{
}

void cSharePointSettingsPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPageEx::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(cSharePointSettingsPage)
	DDX_CBString(pDX, IDC_COMBO1, m_strContainerType);
	DDX_Text(pDX, IDC_EDIT3, m_strOwnerAccount);
	DDX_Text(pDX, IDC_EDIT6, m_strOwnerEMail);
	DDX_Text(pDX, IDC_EDIT8, m_strLDAPDirectory);
	DDX_Text(pDX, IDC_EDIT1, m_strSiteContainerRoot);
	DDX_Check(pDX, IDC_CHECK1, m_bShowInSiteDirectory);
	DDX_Text(pDX, IDC_EDIT9, m_strUserDataFileName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(cSharePointSettingsPage, CPropertyPageEx)
	//{{AFX_MSG_MAP(cSharePointSettingsPage)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// cSharePointSettingsPage message handlers
CString cSharePointSettingsPage::GetLDAPPath()
{

  ULONG connectSuccess = 0;
  ULONG option = 0;

  LDAP* pLdapConnection = NULL;
	LDAPMessage* pMes = NULL;
	LDAPMessage* pEntry = NULL;
	CString cRet,cInfo;
  //-----------------------------------------------------------
  // Initialize a session. LDAP_PORT is the default port, 389.
  //-----------------------------------------------------------
  pLdapConnection = ldap_init(NULL, LDAP_PORT);

  if (pLdapConnection == NULL)
		{
			cInfo.Format( "ldap_init failed with 0x%x.\n",GetLastError());
			AfxMessageBox(cInfo);
			return "";
		}

  //--------------------------------------------------------
  // Connect to the server
  //--------------------------------------------------------
  connectSuccess = ldap_connect(pLdapConnection, NULL);

  if(connectSuccess != LDAP_SUCCESS)
		{
      cInfo.Format( "ldap_connect failed with 0x%x.\n",connectSuccess);
      goto error_exit;
		}
	// Get the RootDSE attributes.
	option  = ldap_search_s( pLdapConnection,
                     "",
                     LDAP_SCOPE_BASE,
                     "(objectClass=*)",
                     NULL,
                     0,
                     &pMes );

	
	pEntry = ldap_first_entry(pLdapConnection, pMes); 
	if(pEntry)
		{

			PCHAR* pV=ldap_get_values(pLdapConnection,pEntry,"defaultNamingContext"); 
			if(pV)
				{
					cRet=*pV;
				}
		}


  // Cleanup.
   if(pMes != NULL)
      ldap_msgfree(pMes);

		//----------------------------------------------------------
    // Normal cleanup and exit
    //----------------------------------------------------------
    ldap_unbind(pLdapConnection);

    return cRet;

    //----------------------------------------------------------
    // On error cleanup and exit
    //----------------------------------------------------------
error_exit:
    ldap_unbind(pLdapConnection);
/*
	LPUNKNOWN pUnk=NULL;
	HRESULT hr = ADsGetObject(L"LDAP://RootDSE", IID_IUnknown, (void**) &pUnk );

	if ( !SUCCEEDED(hr) )
		{
			MessageBox(GetErrorMessage(hr));
			pUnk = NULL;
			return "Not Found";
		}

	IADsPropertyList *m_pPropList;
	m_pPropList = NULL;
	hr = pUnk->QueryInterface( IID_IADsPropertyList, (void **) &m_pPropList );
	if ( !SUCCEEDED(hr) )
	{
		MessageBox(_T("Fatal Error! QI for IADsPropertyList failed"));
		return "Not Found";
	}
	//////////////////////////////////////////
	// Retrieve the data to the cache
	////////////////////////////////////////////
	IADs *pADs=NULL;
	hr = m_pPropList->QueryInterface( IID_IADs, (void**) &pADs );
	if ( SUCCEEDED(hr) )
	{
		hr = pADs->GetInfo();
		pADs->Release();
	}

	VARIANT var;
	IDispatch *pDispatch;
	IADsPropertyEntry *pEntry;
	IADsPropertyValue *pValue;
	LONG  lADsType;
	 
	
	hr = m_pPropList->GetPropertyItem( L"defaultNamingContext", ADSTYPE_CASE_IGNORE_STRING, &var );
	

	if ( SUCCEEDED(hr) )
		{
			pDispatch = V_DISPATCH( &var );
			hr = pDispatch->QueryInterface( IID_IADsPropertyEntry, (void**) &pEntry );
			VariantClear( &var );

			// IADsPropertyEntry
			if ( SUCCEEDED(hr) )
				{
					CPtrList dList;

				// get_Values return array of VT_DISPATH
					hr = pEntry->get_Values( &var );
				 
					
					pEntry->get_ADsType( &lADsType);

					hr = VariantToPtrList( var, dList );

					

				 ////////////////////////////
				 // IADsPropertyValue
				 /////////////////////////////
					if ( SUCCEEDED(hr) )
						{
							POSITION pos;
							pos = dList.GetHeadPosition();
							while ( pos != NULL )
								{
									pDispatch = (IDispatch*) dList.GetAt(pos);
									hr = pDispatch->QueryInterface( IID_IADsPropertyValue, (void**) &pValue );
									if ( SUCCEEDED(hr) )
										{
											pValue->AddRef();
											CString s;
											hr = PropertyValueToString( lADsType, pValue, s );
											cRet="LDAP://CN=Users,"+s;
						
											pValue->Release();
										}
									dList.GetNext(pos);
								}
							
						}

				 dList.RemoveAll();
				 VariantClear(&var);
				 pEntry->Release(); 

			}
		}
	else
		{
			MessageBox(GetErrorMessage(hr));
		}


	m_pPropList->Release();
	pUnk->Release();
	pUnk=NULL;*/
	return cRet;
}

#include "CUserFromFile.h"

LRESULT cSharePointSettingsPage::OnWizardNext() 
{
	theApp.stControl.pDebugLog->LogOneLine("Entering cSharePointSettingsPage::OnWizardNext()");
	theApp.stControl.pDebugLog->Flush();
	UpdateData();
	theApp.stControl.m_strLDAPPath=m_strLDAPDirectory;
	if(NULL==theApp.stControl.pResolver)
		theApp.stControl.pResolver=new cLDAPResolver(m_strLDAPDirectory);
	if("Sites"==m_strContainerType)
		m_strContainerType="Site";
	theApp.stControl.m_strContainerType=m_strContainerType;
	theApp.stControl.bstrContainer=m_strContainerType.AllocSysString();
	theApp.stControl.m_strOwnerAccount=m_strOwnerAccount;
	theApp.stControl.m_strOwnerEMail=m_strOwnerEMail;
	theApp.stControl.bstrSiteContainerRoot=m_strSiteContainerRoot.AllocSysString();
	theApp.stControl.m_bShowInSiteDirectory=m_bShowInSiteDirectory;
	theApp.stControl.m_strUserDataFileName=m_strUserDataFileName ;

	CStdioFile cf;
	CString cData;
	if(cf.Open(m_strUserDataFileName ,CFile::modeRead ))
		{
			while(cf.ReadString(cData))
				{
					int Pos=cData.Find(",");
					if(Pos>0)
						{
							
							CUserFromFile *cUserFromFile=new CUserFromFile();
							cUserFromFile->m_strSMTPAddress=cData.Left(Pos);
							cUserFromFile->m_strAccount=cData.Mid(Pos+1);
							theApp.stControl.m_objUsersFromFile.Add(cUserFromFile);

						}

				}
			cf.Close();
			theApp.stControl.m_objUsersFromFile.Sort();

		}
	theApp.stControl.pDebugLog->LogOneLine("Exiting cSharePointSettingsPage::OnWizardNext()");
	theApp.stControl.pDebugLog->Flush();


	return CPropertyPageEx::OnWizardNext();
}

BOOL cSharePointSettingsPage::OnSetActive() 
{
	theApp.stControl.pDebugLog->LogOneLine("Entering cSharePointSettingsPage::OnSetActive()");
	theApp.stControl.pDebugLog->Flush();
	m_strLDAPDirectory =GetLDAPPath();
	UpdateData(FALSE);
	
	theApp.stControl.pDebugLog->LogOneLine("Exiting cSharePointSettingsPage::OnSetActive()");
	theApp.stControl.pDebugLog->Flush();
	return CPropertyPageEx::OnSetActive();
}

void cSharePointSettingsPage::OnBrowse() 
{
		UpdateData();
		CFileDialog dlg(TRUE, "", "",  OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,  "User files (*.csv)|*.csv|All Files (*.*)|*.*||", this);
 
		dlg.m_ofn.lpstrTitle = "Open";

		if (dlg.DoModal() == IDOK)
			m_strUserDataFileName=dlg.GetPathName();
		UpdateData(FALSE);

	
	
}
